﻿using Infrastructure.DbUtility;
using Infrastructure.Utility;
using System;
using System.Data;
using System.IO;
public partial class index : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Context.Items["showNav"] = false;

        if (!IsPostBack) this.InitData();
    }

    private void InitData()
    {
        DbHelper db = new DbHelper();
        var ds = db.ExecQuery("SELECT * FROM Advertisment (NOLOCK) WHERE adtype = 0");
        var list = ds.AsEnumerable().Select(r => new { url = r.Value("adurl"), title = r.Value("adtitle"), image = "/" + GetSitePath(r.Value("adimage")), description = r.Value("addescription") });
        lstAds.DataSource = list;
        lstAds.DataBind();



        var tbHots = db.ExecQueryEx("SELECT * FROM [Subject] (NOLOCK) WHERE [hot] = 1 and [status] = 1");
        this.listHot.DataSource = tbHots.AsEnumerable().Select(r => new { img = GetDefaultSubjectImage(r.Value("image")), sid = r.Value<int>("sid"), title = r.Value("name") });
        this.listHot.DataBind();


        var tbRecomms = db.ExecQueryEx("SELECT * FROM [Subject] (NOLOCK) WHERE [recommand] = 1 and [status] = 1");
        this.listRecommend.DataSource = tbHots.AsEnumerable().Select(r => new { img = GetDefaultSubjectImage(r.Value("image")), sid = r.Value<int>("sid"), title = r.Value("name") });
        this.listRecommend.DataBind();

    }


    protected string GetDefaultSubjectImage(string img)
    {
        if (string.IsNullOrWhiteSpace(img) || !File.Exists(img)) return "/content/images/course-108-128.png";
        else return GetSitePath(img);
    }

}